package com.wukang.photovoltaicinspectionpictures.ui.task

import android.content.Intent
import android.os.Build
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.wukang.photovoltaicinspectionpictures.R
import com.wukang.photovoltaicinspectionpictures.base.BaseActivity
import com.wukang.photovoltaicinspectionpictures.bean.TaskData
import com.wukang.photovoltaicinspectionpictures.databinding.ActivityTaskBinding
import com.wukang.photovoltaicinspectionpictures.ui.UiConstants
import com.wukang.photovoltaicinspectionpictures.ui.photograph.TakePhotographActivity
import com.wukang.photovoltaicinspectionpictures.utils.onDebounceClick

/**
 * 任务界面
 * @author wukang
 */
class TaskActivity : BaseActivity<ActivityTaskBinding, TaskViewModel>() {
    /**
     * 任务数据
     */
    private var taskData: TaskData? = null

    override fun initViewBinding(): ActivityTaskBinding = ActivityTaskBinding.inflate(layoutInflater)

    override fun initViewModel(): TaskViewModel = ViewModelProvider(this)[TaskViewModel::class.java]

    override fun initActivity() {
        taskData = intent.extras?.run {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                getParcelable(UiConstants.BUNDLE_KEY_TASK_DATA, TaskData::class.java)
            } else {
                @Suppress("DEPRECATION")
                getParcelable(UiConstants.BUNDLE_KEY_TASK_DATA)
            }
        }.apply {
            if (this != null) {
                mBinding.tvTitle.text = taskName
                mBinding.tvPowerStationName.text = getString(R.string.task_power_station_name, powerStationName)
                mBinding.tvPhotographLocation.text = getString(R.string.task_photograph_location, photographLocation)
                mBinding.tvPhotographNotes.text = getString(R.string.task_photograph_notes, photographNotes)
                mBinding.tvTestTime.text = getString(R.string.task_test_time, testTime.toString(10))
                mBinding.llLevel.visibility = View.VISIBLE
                mBinding.tvLevelHigh.text = getString(R.string.task_level_high, levelHigh)
                mBinding.tvLevelMid.text = getString(R.string.task_level_mid, levelMid)
                mBinding.tvLevelLow.text = getString(R.string.task_level_low, levelLow)
                mBinding.tvLevelNormal.text = getString(R.string.task_level_normal, levelNormal)
            } else {
                mBinding.tvTitle.setText(R.string.task_new_task)
                getString(R.string.task_data_none).also {
                    mBinding.tvPowerStationName.text = getString(R.string.task_power_station_name, it)
                    mBinding.tvPhotographLocation.text = getString(R.string.task_photograph_location, it)
                    mBinding.tvPhotographNotes.text = getString(R.string.task_photograph_notes, it)
                    mBinding.tvTestTime.text = getString(R.string.task_test_time, it)
                }
                mBinding.llLevel.visibility = View.GONE
            }
        }
    }

    override fun ActivityTaskBinding.initView() {
        tvLevelHigh.onDebounceClick { setLevelStyle(it) }
        tvLevelMid.onDebounceClick { setLevelStyle(it) }
        tvLevelLow.onDebounceClick { setLevelStyle(it) }
        tvLevelNormal.onDebounceClick { setLevelStyle(it) }

        ivBack.onDebounceClick { finish() }
        btTakePhotograph.onDebounceClick {
            startActivity(Intent(this@TaskActivity, TakePhotographActivity::class.java))
        }
    }

    private fun setLevelStyle(view: View) {
        mBinding.tvLevelHigh.apply { isSelected = id == view.id }
        mBinding.tvLevelMid.apply { isSelected = id == view.id }
        mBinding.tvLevelLow.apply { isSelected = id == view.id }
        mBinding.tvLevelNormal.apply { isSelected = id == view.id }
    }
}